package org.tgcloud.group.service.manager.notify;

import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.entity.SystemNotifyTemplate;
import org.tgcloud.group.repository.SystemNotifyTemplateMapper;
import org.tgcloud.group.web.controller.dto.notify.SystemNotifyTemplateDTO;
import org.tgcloud.group.web.controller.req.notify.notifyTemplateReq.NotifyTemplatePageReqVO;
import org.tgcloud.group.web.controller.req.notify.notifyTemplateReq.NotifyTemplateSaveReqVO;

import cn.hutool.core.bean.BeanUtil;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author: zhy
 * @Created: 2025-09-19-上午11:44
 * @Description:
 */
@Service
public class NotifyTemplateService {


    /**
     * 正则表达式，匹配 {} 中的变量
     */
    private static final Pattern PATTERN_PARAMS = Pattern.compile("\\{(.*?)}");

    @Resource
    private SystemNotifyTemplateMapper notifyTemplateMapper;

    @Autowired
    private NotifyService notifyService;

    public String toJSONTemplateContentParams(String content) {
        return JSON.toJSONString(ReUtil.findAllGroup1(PATTERN_PARAMS, content));
    }

    public APPResultMessage createNotifyTemplate(NotifyTemplateSaveReqVO createReqVO) {
        // 校验站内信编码是否重复
        SystemNotifyTemplate systemNotifyTemplate = notifyTemplateMapper.selectMsgByCode(createReqVO.getCode());
        if (systemNotifyTemplate != null){
            return new APPResultMessage(6001, "该模版编码已被使用");
        }

        // 插入
        SystemNotifyTemplate notifyTemplate = BeanUtil.toBean(createReqVO, SystemNotifyTemplate.class);
        notifyTemplate.setParams(toJSONTemplateContentParams(notifyTemplate.getContent()));
        notifyTemplate.setCreateTime(new Date());
        notifyTemplateMapper.insertSelective(notifyTemplate);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage updateNotifyTemplate(NotifyTemplateSaveReqVO updateReq) {
        // 校验站内信编码是否重复
        SystemNotifyTemplate systemNotifyTemplate = notifyTemplateMapper.selectMsgByCodeAndNotId(updateReq.getCode(), updateReq.getId());
        if (systemNotifyTemplate != null){
            return new APPResultMessage(6001, "该模版编码已被使用");
        }

        // 更新
        SystemNotifyTemplate updateObj = BeanUtil.toBean(updateReq, SystemNotifyTemplate.class);
        updateObj.setParams(toJSONTemplateContentParams(updateObj.getContent()));
        updateObj.setUpdateTime(new Date());
        notifyTemplateMapper.updateByPrimaryKeySelective(updateObj);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage deleteNotifyTemplate(Long id) {
        notifyTemplateMapper.updateDeletedById(id);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage getNotifyTemplate(Long id) {
        SystemNotifyTemplate systemNotifyTemplate = notifyTemplateMapper.selectByPrimaryKey(id);
        APPResultMessage message = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(systemNotifyTemplate);
        message.setResult(resultMessage);
        return message;
    }

    public APPResultMessage getNotifyTemplatePage(NotifyTemplatePageReqVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        List<SystemNotifyTemplateDTO> list = notifyTemplateMapper.selectManageList(pageVO.getName(), pageVO.getCode(), pageVO.getStatus());
        PageInfo<SystemNotifyTemplateDTO> tables = new PageInfo<>(list);
        ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
        APPResultMessage<Object, SystemNotifyTemplateDTO> message = APPResultMessage.getSuccessMessage();
        message.setResult(result);
        return message;
    }

    public APPResultMessage doSendNotifyTest(Long id) {
        Map<String, Object> templateParams = new HashMap<>();
        notifyService.doSendNotify(129, (short) 2, "addProject", templateParams, 305, (short) 3, 5465);
        return APPResultMessage.getSuccessMessage();
    }
}
